﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DK.BinarySearch
{
    public class SquareRoot
    {


        public SquareRoot() { }

        public int GetSquare(int total) 
        {
            var Min = 1;
            var Max = total - 1;
            var Mid = Min + (Max - Min) / 2;

            while (Min < Mid && Max > Mid)
            {
                if ((Mid * Mid) < total)
                {
                    Min = Mid;
                }
                else if ((Mid * Mid) > total)
                {
                    Max = Mid;
                }
                else if ((Mid * Mid) == total) 
                {
                    return Mid;
                }

                Mid = (Min + (int)Math.Ceiling((double)(Max - Min) / 2));
            }
            return Mid;
        }
    }
}
